/*
 * Copyright (C) 2001-2011 Jacek Sieka, arnetheduck on gmail point com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifndef DCPLUSPLUS_DCPP_HTTP_CONNECTION_H
#define DCPLUSPLUS_DCPP_HTTP_CONNECTION_H

#include "BufferedSocket.h"
#include "Streams.h"

namespace dcpp {

class HttpConnection;

class HttpConnectionListener
{
public:
	virtual ~HttpConnectionListener() { }
	template<int I>	struct X { enum { TYPE = I };  };

	typedef X<0> Failed;
	typedef X<1> Abort;
	typedef X<2> Complete;
	typedef X<3> Redirected;

	virtual void on(Failed, HttpConnection*, const string&) noexcept { }
	virtual void on(Abort, HttpConnection*, const string&) noexcept { }
	virtual void on(Complete, HttpConnection*, const string&, bool) noexcept { }
	virtual void on(Redirected, HttpConnection*, const string&) noexcept { }
};

class HttpConnection : BufferedSocketListener, public Speaker<HttpConnectionListener>, private boost::noncopyable
{
public:
	enum CoralizeStates { CST_DEFAULT, CST_CONNECTED, CST_NOCORALIZE };

	HttpConnection(MutableOutputStream* tgt, CoralizeStates aCoralState = CST_DEFAULT, const string& aUserAgent = Util::emptyString);
	HttpConnection(string& tgt, CoralizeStates aCoralState = CST_DEFAULT, const string& aUserAgent = Util::emptyString);

	virtual ~HttpConnection() { if(socket) abortRequest(true); }

	void downloadFile(const string& aFile);
	void postData(const string& aFile, const StringMap& aData);
	void abort();

	/** These methods are not safe to call mid-connection */
	void setCoralizeState(CoralizeStates state) { coralizeState = state; }
	void setUserAgent(const string& aUserAgent = Util::emptyString) { userAgent = aUserAgent; connState = CONN_UNKNOWN; }
	void setOutput(MutableOutputStream* tgt) { dcassert(out == NULL); out.reset(tgt); }

	const string& getURL() const { return currentUrl; }
	const string& getMIME() const { return mimeType; }

	int64_t getSize() const { return size; }
	int64_t getDone() const { return done; }

	bool isCoral() const { return BOOLSETTING(CORAL) && coralizeState != CST_NOCORALIZE; }

private:
	enum RequestType { TYPE_GET, TYPE_POST };
	enum ConnectionStates { CONN_UNKNOWN, CONN_OK, CONN_FAILED, CONN_MOVED, CONN_CHUNKED };

	string currentUrl;
	string userAgent;
	string method;
	string file;
	string server;
	uint16_t port;

	string requestBody;

	string mimeType;
	int64_t size;
	int64_t done;

	ConnectionStates connState;
	CoralizeStates coralizeState;
	RequestType connType;

	BufferedSocket* socket;
	std::unique_ptr<MutableOutputStream> out;

	void prepareRequest(RequestType type);
	void abortRequest(bool disconnect);

	virtual void notifyAborted() noexcept { fire(HttpConnectionListener::Abort(), this, currentUrl); }
	virtual void notifyCompleted() noexcept { fire(HttpConnectionListener::Complete(), this, currentUrl, BOOLSETTING(CORAL) && coralizeState != CST_NOCORALIZE); }
	virtual void notifyFailed(const string& aError) noexcept { fire(HttpConnectionListener::Failed(), this, aError + " (" + currentUrl + ")"); }
	virtual void notifyRedirected(const string& aLocation) noexcept { fire(HttpConnectionListener::Redirected(), this, aLocation); }

	// BufferedSocketListener
	void on(Connected) noexcept;
	void on(Line, const string&) noexcept;
	void on(Data, uint8_t*, size_t) noexcept;
	void on(ModeChange) noexcept;
	void on(Failed, const string&) noexcept;

};

} // namespace dcpp

#endif // !defined(HTTP_CONNECTION_H)

/**
 * @file
 * $Id: HttpConnection.h 1034 2011-08-21 09:51:11Z crise $
 */
